package com.lin.pay.paypal.v1;

import com.google.gson.Gson;
import com.lin.pay.paypal.v1.vo.CreatePaymentDto;
import com.lin.pay.paypal.v1.vo.CreatePaymentReqVo;
import com.lin.pay.paypal.v1.vo.ShippingAddress;
import com.lin.pay.paypal.v1.vo.TransactionAmountDto;
import com.lin.pay.paypal.v1.vo.TransactionDetailsDto;
import com.lin.pay.paypal.v1.vo.TransactionDto;
import com.lin.pay.paypal.v1.vo.TransactionItemDetailsDto;
import com.lin.pay.paypal.v1.vo.TransactionItemList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class Helper {
    public static CreatePaymentDto getCreatePaymentData(CreatePaymentReqVo cp, String returnUrl, String cancelUrl) {
        // set redirect urls
        //CreatePaymentReqVo cp = g.fromJson(dataFromHtml, CreatePaymentReqVo.class);
        Map<String, String> redirects = new HashMap<>();
        redirects.put("return_url", returnUrl);
        redirects.put("cancel_url", cancelUrl);

        //set payer data
        Map<String, String> payer = new HashMap<String, String>();
        payer.put("payment_method", "paypal");
        //payer.put("external_selected_funding_instrument_type", "CREDIT");

        // built an transaction data object

        TransactionItemDetailsDto transactionItemDetailsDto = new TransactionItemDetailsDto();
        transactionItemDetailsDto.setQuantity(cp.getQuantity());
        transactionItemDetailsDto.setName(cp.getName());
        transactionItemDetailsDto.setPrice(cp.getPrice());
        transactionItemDetailsDto.setCurrency(cp.getCurrency());
        transactionItemDetailsDto.setDescription(cp.getDescription());
        transactionItemDetailsDto.setTax(cp.getTax());

        ArrayList<TransactionItemDetailsDto> arrayOfItems = new ArrayList<>();
        arrayOfItems.add(transactionItemDetailsDto);

        //set address
//        ShippingAddress shippingAddress = new ShippingAddress();
//        if (cp.getCustomFlag().equals("true")) {
//            shippingAddress.setRecipient_name(cp.getRecipient_name());
//            shippingAddress.setLine1(cp.getLine1());
//            shippingAddress.setLine2(cp.getLine2());
//            shippingAddress.setCity(cp.getCity());
//            shippingAddress.setCountry_code(cp.getCountry_code());
//            shippingAddress.setPhone(cp.getPhone());
//            shippingAddress.setPostal_code(cp.getPostal_code());
//            shippingAddress.setState(cp.getState());
//        }

        TransactionItemList transactionItemList = new TransactionItemList();
        transactionItemList.setItems(arrayOfItems);
//        if (cp.getCustomFlag().equals("true")) {
//            transactionItemList.setShipping_address(shippingAddress);
//        }
        // set amount
        TransactionDetailsDto transactionDetailsDto = new TransactionDetailsDto();
        transactionDetailsDto.setShipping(cp.getShipping());
        transactionDetailsDto.setSubtotal(String.valueOf(Float.parseFloat(cp.getPrice())*Integer.parseInt(cp.getQuantity())));
        transactionDetailsDto.setTax(cp.getTax());
        transactionDetailsDto.setShipping_discount(cp.getShipping_discount());
        transactionDetailsDto.setHandling_fee(cp.getHandling_fee());
        transactionDetailsDto.setInsurance(cp.getInsurance());


        TransactionAmountDto transactionAmountDto = new TransactionAmountDto();
        transactionAmountDto.setCurrency(cp.getCurrency());
        transactionAmountDto.setDetails(transactionDetailsDto);
        // total=shipping+subtotal+tax
        transactionAmountDto.setTotal(String.valueOf(Float.parseFloat(transactionDetailsDto.getSubtotal())+Float.parseFloat(cp.getShipping())+Float.parseFloat(cp.getTax())));

        CreatePaymentDto createPaymentDto = new CreatePaymentDto();
        createPaymentDto.setId(cp.getId());
        createPaymentDto.setIntent(cp.getIntent());
        createPaymentDto.setRedirect_urls(redirects);
        createPaymentDto.setPayer(payer);

        TransactionDto transactionDto = new TransactionDto();
        transactionDto.setAmount(transactionAmountDto);
        transactionDto.setItem_list(transactionItemList);


        transactionDto.setDescription(cp.getDescription());
        transactionDto.setInvoice_number(generateInvoiceIDString());
        transactionDto.setCustom("Some Sample Data");

        ArrayList<TransactionDto> arrayOfTransaction = new ArrayList<>();
        arrayOfTransaction.add(transactionDto);
        createPaymentDto.setTransactions(arrayOfTransaction);
        return createPaymentDto;
    }

    public static String generateInvoiceIDString() {
        String uuid = UUID.randomUUID().toString();
        return "InvoiceID = " + uuid;
    }
}
